package com.neusoft.bd.myflink.step06.wmd;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Arrays;

public class WindowJoinDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.setParallelism(2);
//        l

        SingleOutputStreamOperator<Tuple2<String, Integer>> leftDS = environment.fromCollection(Arrays.asList(
                Tuple2.of("a", 1),
                Tuple2.of("b", 2),
                Tuple2.of("c", 1),
                Tuple2.of("d", 1))).assignTimestampsAndWatermarks(
                WatermarkStrategy.<Tuple2<String, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((value, ts) -> value.f1 * 1000l));

        SingleOutputStreamOperator<Tuple3<Integer, String, String>> rightDS = environment.fromCollection(Arrays.asList(
                Tuple3.of(1, "a", "aaa"),
                Tuple3.of(2, "b", "2222"),
                Tuple3.of(3, "c", "333"),
                Tuple3.of(40, "a", "444")))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Tuple3<Integer, String, String>>forMonotonousTimestamps()
                                .withTimestampAssigner((value, ts) -> value.f0 * 1000l));

        leftDS.keyBy(t -> t.f0)
                .intervalJoin(rightDS.keyBy(t -> t.f1))
                .between(Time.seconds(-2), Time.seconds(2))
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<Integer, String, String>, String>() {
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<Integer, String, String> right, Context ctx,
                                               Collector<String> out) throws Exception {
                        out.collect("left ->" + left + ",Right->" + right);
                    }
                })
                .print();


        environment.execute();
    }

    public static void main2(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.setParallelism(2);
//        l

        SingleOutputStreamOperator<Tuple2<String, Integer>> leftDS = environment.fromCollection(Arrays.asList(
                Tuple2.of("a", 1),
                Tuple2.of("b", 2),
                Tuple2.of("c", 1),
                Tuple2.of("d", 1))).assignTimestampsAndWatermarks(
                WatermarkStrategy.<Tuple2<String, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((value, ts) -> value.f1 * 1000l));

        SingleOutputStreamOperator<Tuple3<Integer, String, String>> rightDS = environment.fromCollection(Arrays.asList(
                Tuple3.of(1, "a", "aaa"),
                Tuple3.of(2, "b", "2222"),
                Tuple3.of(3, "c", "333"),
                Tuple3.of(40, "a", "444")))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Tuple3<Integer, String, String>>forMonotonousTimestamps()
                                .withTimestampAssigner((value, ts) -> value.f0 * 1000l));

        leftDS.keyBy(t -> t.f0)
                .intervalJoin(rightDS.keyBy(t -> t.f1))
                .between(Time.seconds(-2), Time.seconds(2))
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<Integer, String, String>, String>() {
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<Integer, String, String> right, Context ctx,
                                               Collector<String> out) throws Exception {
                        out.collect("left ->" + left + ",Right->" + right);
                    }
                })
                .print();


        environment.execute();
    }

    public static void main1(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.setParallelism(2);
//        l

        SingleOutputStreamOperator<Tuple2<String, Integer>> leftDS = environment.fromCollection(Arrays.asList(
                Tuple2.of("a", 1),
                Tuple2.of("b", 2),
                Tuple2.of("c", 1),
                Tuple2.of("d", 1))).assignTimestampsAndWatermarks(
                WatermarkStrategy.<Tuple2<String, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((value, ts) -> value.f1 * 1000l));

        SingleOutputStreamOperator<Tuple3<Integer, String, String>> rightDS = environment.fromCollection(Arrays.asList(
                Tuple3.of(1, "a", "aaa"),
                Tuple3.of(2, "b", "2222"),
                Tuple3.of(3, "c", "333"),
                Tuple3.of(40, "a", "444")))
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Tuple3<Integer, String, String>>forMonotonousTimestamps()
                                .withTimestampAssigner((value, ts) -> value.f0 * 1000l));

        DataStream<Object> apply = leftDS.join(rightDS)
                //左侧取值计算
                .where(left -> left.f0)
//                右侧
                .equalTo(right -> right.f1)
//                满足条件的记录开窗
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
//                进入相同窗口的记录执行Join
                .apply(new JoinFunction<Tuple2<String, Integer>, Tuple3<Integer, String, String>, Object>() {
                    @Override
                    public Object join(Tuple2<String, Integer> left, Tuple3<Integer, String, String> right) throws Exception {
                        return "left ->" + left + ",Right->" + right;
                    }
                });
        apply.print();
        environment.execute();
    }
}
